package com.github.lee.unsplash.ui.topic

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.github.lee.unsplash.R
import com.github.lee.unsplash.databinding.ItemMainTopicBinding
import com.github.lee.unsplash.domain.TopicBean

class HomeTopicAdapter :
    BaseQuickAdapter<TopicBean, BaseDataBindingHolder<ItemMainTopicBinding>>(R.layout.item_main_topic),
    LoadMoreModule {
    override fun convert(holder: BaseDataBindingHolder<ItemMainTopicBinding>, item: TopicBean) {
        holder.dataBinding?.apply {
            topic = item
            val owners = item.owners
            avatar = if (owners.isNullOrEmpty()) {
                ""
            } else {
                owners[0].profileImage.large
            }
            val sb = StringBuilder()
            for ((index, owner) in owners.withIndex()) {
                sb.append(owner.name)
                if (index < owners.size - 1) {
                    sb.append(", ")
                }
            }
            ownerNames = sb.toString()
        }
    }
}